//
//  LBFoundationMacroDefinition.h
//  LBFoundation
//
//  Created by WuShiHai on 19/5/24.
//  Copyright (c) 2019 Lebbay. All rights reserved.
//

#ifndef LBFoundationMacroDefinition_h
#define LBFoundationMacroDefinition_h

//MainBundle
#define MainBundle()                                ([NSBundle mainBundle])
#define PathForBundleResource(resName, resType)     [MainBundle() pathForResource:(resName) ofType:(resType)]
#define URLForBundleResource(resName, resType)      [MainBundle() URLForResource:(resName) \
withExtension:(resType)]
#define APPDisplayName()                            [MainBundle() \
objectForInfoDictionaryKey:@"CFBundleDisplayName"]
#define AppBundleIdentifier()                       [MainBundle() \
objectForInfoDictionaryKey:@"CFBundleIdentifier"]
#define AppReleaseVersionNumber()                   [MainBundle() \
objectForInfoDictionaryKey:@"CFBundleShortVersionString"]
#define AppBuildVersionNumber()                     [MainBundle() objectForInfoDictionaryKey:@"CFBundleVersion"]

//text size
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 70000
#define LB_MULTILINE_TEXTSIZE(text, font, maxSize, mode) [text length] > 0 ? [text \
boundingRectWithSize:maxSize options:(NSStringDrawingUsesLineFragmentOrigin) \
attributes:@{NSFontAttributeName:font} context:nil].size : CGSizeZero;
#else
#define LB_MULTILINE_TEXTSIZE(text, font, maxSize, mode) [text length] > 0 ? [text \
sizeWithFont:font constrainedToSize:maxSize lineBreakMode:mode] : CGSizeZero;
#endif

//block
#define LBBlockSafeRun(block, ...) block ? block(__VA_ARGS__) : nil
#define LBBlockSafeRunInMain(block, ...)  \
    dispatch_async(dispatch_get_main_queue(), ^{    \
        LBBlockSafeRun(block, __VA_ARGS__);  \
    })

//获取通知中心
#define LBNotificationCenter [NSNotificationCenter defaultCenter]

//自定义高效率的 NSLog
#ifdef DEBUG
#define NSLog(...) DDLogInfo(@"%@\n\n", [NSString stringWithFormat:__VA_ARGS__])
#else
#define NSLog(...)
#endif

//判断当前的系统版本
//获取系统版本
#define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
//判断 iOS 8 或更高的系统版本
#define IOS_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0)? (YES):(NO))

//判断是真机还是模拟器
#if TARGET_OS_IPHONE
#endif
#if TARGET_IPHONE_SIMULATOR
#endif

//沙盒目录文件
//获取temp
#define LBPathTemp NSTemporaryDirectory()
//获取沙盒 Document
#define LBPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
//获取沙盒 Cache
#define LBPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

//6.弱引用/强引用
#define weakSelf(type)  __weak typeof(type) weak##type = type;
#define strongSelf(type)  __strong typeof(type) type = weak##type;

//由角度转换弧度 由弧度转换角度
#define LBDegreesToRadian(x) (M_PI * (x) / 180.0)
#define LBRadianToDegrees(radian) (radian*180.0)/(M_PI)

static inline NSString * LBFormatString(NSString * _Nonnull format, ...) {
    va_list args;
    
    if (format) {
        va_start(args, format);
        return [[NSString alloc] initWithFormat:format arguments:args];
        va_end(args);
    }
    
    return nil;
}

static inline BOOL LBRunningInAppExtension(void) {
    return [[[[NSBundle mainBundle] bundlePath] pathExtension] isEqualToString:@"appex"];
}

#endif /* LBFoundationMacroDefinition_h */
